<template>
<view class="love">
		<div class="top">
			<div class="left">
				<img src="../../static/icon_cha.png" alt="">
				<!-- <span>已收藏{{$store.state.list.length}}个餐宴</span> -->
				<span>已收藏{{list.length}}个餐宴</span>
			</div>
			<div class="right">
				<img src="../../static/icon_clear.png" alt="">
				<!-- <span @click="CLEARLIST">清空</span> -->
				<span @click="clear_list">清空</span>
			</div>
		</div>
		
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog 
				mode="base" 
				title="清空收藏" 
				content="清空收藏后,将无法恢复"
				:duration="1000" 
				:before-close="true" 
				@close="close" 
				@confirm="confirm">
			</uni-popup-dialog>
		</uni-popup>
		
		<div class="empty" v-if="list.length==0">
			<img src="../../static/nomore2.png" alt="">
		</div>
		<!-- <LoveItem :info="list" @del-love="handleCancel"></LoveItem> -->
		
		<!-- a4:使用 store中的数据 -->
		<LoveItem  @del-love="handleCancel"></LoveItem>
	</view>
</template>

<script>
	import LoveItem from "@/components/LoveItem"
	import {mapMutations,mapState} from "vuex";//辅助函数
	export default {
		data() {
			return {
				// list:[]
				page:1 //默认第2页
			};
		},
		computed:{
			...mapState(["list"])
		},
		components:{
			LoveItem
		},
		methods:{
			...mapMutations(['SETLIST','CLEARLIST']),
			handleCancel(num){
				console.log("2.父组件接收到了子组件的数据",num);
				//根据下标，删除列表数据
				this.list.splice(num,1);
			},
			//清空3个函数
			clear_list(){
				this.$refs.popup.open();
				// this.CLEARLIST()
			},
			close(){
				console.log("cancel")
				this.$refs.popup.close()//关闭对话框
			},
			confirm(){
				console.log("确定-要删除")
				this.CLEARLIST()//确定-要删除
				this.$refs.popup.close()//关闭对话框
			}
		},
		/* created() {
			
		} */
		async onReachBottom(){
			console.log('到底了。别啦了')
			// 显示加载动画
			uni.showLoading({title:"努力加载数据中..."})
			//默认清空加载页列表的第1页，如果触底了，加载第2页,从后端获取数据。
			//追加 数据到当前列表的尾部，滚动一下屏幕
			// var page = 1;
			this.page++;
			
			try{
				/* console.log("page1:",this.page)
				this.$nextTick(()=>{
					console.log("page2:",this.page)
				}) */
				
				//正常
				let ret = await uni.request({url:"http://api.brqc.com.cn/shxmp/goodslist?page="+this.page});
				console.log(ret,ret[1].data.data)
				
				if(this.page > 3){
					console.log("我是有底线的...")
					uni.showToast({
						title:"我是有底线的,没有数据了^_^"
					})
					return;
				}
				
				let remote_list = ret[1].data.data;
				let new_list = [...this.list,...remote_list]; //新数组
				console.log(new_list)
				this.SETLIST(new_list)
				
				uni.hideLoading()
				
			} catch(err){
				//异常
				console.log("err",err)
			}
		},
		onPullDownRefresh(){
			uni.startPullDownRefresh({
				success() {
					console.log("开始下拉...")
					//加载动画，请求数据，设置数据
					
					//异步请求
					setTimeout(()=>{
						uni.stopPullDownRefresh()//停止下拉
						//关闭动画
					},3000)
				}
			})
			
			
		},
		onShow(){
			uni.showLoading({title:"努力加载数据中..."})
			
			uni.request({
				url:"http://api.brqc.com.cn/shxmp/goodslist"
			})
			.then(res=>{
				/* uni.showToast({
					title:"获取完成"
				}) */
				console.log('心愿单',res)
				let result = res[1].data.data
				// this.list = result;
				//a2:保存数据到store
				// this.$store.commit("SETLIST",result)
				this.SETLIST(result)//相调用当前组件中自己的函数一样
				uni.hideLoading()
			})
		}
	}
</script>

<style lang="less" scoped>
.love{
	background-color: #fff;
	width: 100%;
	height: 100%;
	
	.empty{
		position: relative;
		img{
			width: 280rpx;
			height: 238rpx;
			position: absolute;
			top:100rpx;
			left:50%;
			margin-left: -140rpx;
		}
	}
	.top{
		border-top: 1px solid #ccc;
		display: flex;
		justify-content: space-between;
		padding: 30rpx;
		
		.left{
			display: flex;
			align-items: center;
			img{
				width: 30rpx;
				height: 30rpx;
			}
			span{
				font-size: 30rpx;
				margin-left: 20rpx;
			}
		}
		
		.right{
			display: flex;
			align-items: center;
			img{
				width: 30rpx;
				height: 30rpx;
			}
			span{
				font-size: 30rpx;
				margin-left: 10rpx;
				color: #ccc;
			}
		}
	}
}
</style>